#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244853;
typedef long long LL;
const int N = 2e6 + 10;

void solve() {
    int l,r;
    cin >> l >> r;
    map<int,int> mp;
    int res = 0;
    for(int i = r; i >= l ; i --)
    {
        if(mp[i] == 0)
        {
            int tt = i;
            vector<int> anss;
            while(tt)
            {
                anss.push_back(tt % 2);
                tt /= 2;
            }
            int ans = 0;
            int q = 1;
            for(int j = 0; j < anss.size(); j ++)
            {
                if(anss[j] == 0)
                {
                    ans += q;
                }
                q *= 2;
            }
            mp[i] = ans;
            mp[ans] = i;
            res += ans | i;
        }
    }
    cout << res * 2 << endl;
    for(int i = l; i <= r; i ++)
    {
        cout << mp[i] << ' ';
    }
    cout << endl;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
}
/*
function find(int x):    
l := 1    
r := n    
while l <= r:        
let m be a random integer between l and r,inclusive        
if p[m] == x            
return m       
if p[m] > x:            
    r := m - 1        
else:            
    l := m + 1    
return undefined     // not found
 */